<template>
    <!--管理员-->
    <div class="role">
        <a-card>
            <Search :columns="searchColumns" :search.sync="search" :filter="filter" ex :changeExport="changeExport" />
        </a-card>
        <a-card>
            <div class="table">
                <Tables
                    ref="table"
                    :columns="columns"
                    :formColumns="formColumns"
                    :loadData="findOrderGoodsList"
                    :addShow="false"
                    :delShow="false"
                    :search="search"
                    :editData="editOrderGoods"
                >
                    <template slot="sku" slot-scope="{ record }">
                        <span>{{ record.sku_properties_name.map(items => items.v).join(',') }}</span>
                    </template>
                    <template slot="created" slot-scope="{ record }">
                        <span>{{ record.order.created }}</span>
                    </template>
                    <template slot="delivery_end_time" slot-scope="{ record }">
                        <span v-if="record.order.address">{{ record.order.address.delivery_end_time }}</span>
                    </template>
                </Tables>
            </div>
        </a-card>
    </div>
</template>

<script>
import { findOrderGoodsList, exportOrderGoods, editOrderGoods } from '@/api/order'

export default {
    name: 'GoodsList',
    data() {
        return {
            columns: [
                {
                    dataIndex: 'order.tid',
                    title: '订单号',
                    align: 'center'
                },
                {
                    dataIndex: 'title',
                    title: '商品名称',
                    align: 'center'
                },
                {
                    dataIndex: 'sku',
                    title: '口味,尺寸',
                    align: 'center',
                    scopedSlots: { customRender: 'sku' }
                },
                {
                    dataIndex: 'price',
                    title: '商品原价',
                    align: 'center'
                },
                {
                    dataIndex: 'num',
                    title: '商品数量',
                    align: 'center'
                },
                {
                    dataIndex: 'goods_url',
                    title: '商品详情链接',
                    align: 'center'
                },
                {
                    dataIndex: 'created',
                    title: '订单创建时间',
                    align: 'center',
                    scopedSlots: { customRender: 'created' }
                },
                {
                    dataIndex: 'delivery_end_time',
                    title: '订单送达时间',
                    align: 'center',
                    scopedSlots: { customRender: 'delivery_end_time' }
                }
            ],
            formColumns: {
                title: {
                    type: 'input',
                    label: '商品名称'
                },
                sku_properties_name: {
                    type: 'inputArrayJson',
                    label: '口味,尺寸'
                },
                buyer_messages: {
                    type: 'input',
                    inputType: 'textarea',
                    label: '商品留言'
                },
                id: {
                    type: 'hidden'
                }
            },
            searchColumns: {
                tid: {
                    type: 'input',
                    label: '订单号'
                },
                title: {
                    type: 'input',
                    label: '商品名称'
                },
                outer_sku_id: {
                    type: 'input',
                    label: '商品编码'
                },
                created: {
                    type: 'range',
                    label: '创建时间'
                },
                success_time: {
                    type: 'range',
                    label: '完成时间'
                },
                delivery_time: {
                    type: 'range',
                    label: '送达时间'
                }
            },
            search: {},
            form: {},
            findOrderGoodsList,
            editOrderGoods
        }
    },
    created() {},
    methods: {
        filter(record) {
            if (record.created) {
                const [start, end] = record.created
                record.create_start = start
                record.create_end = end
            }
            if (record.success_time) {
                const [start, end] = record.success_time
                record.success_start = start
                record.success_end = end
            }
            if (record.delivery_time) {
                const [start, end] = record.delivery_time
                record.delivery_start = start
                record.delivery_end = end
            }
            return record
        },
        async changeExport(record) {
            let params = '1=1'
            const form = {}
            Object.keys(record)
                .filter(key => {
                    if (record[key]) {
                        if (!Array.isArray(record[key])) {
                            return true
                        }
                    }
                    return false
                })
                .forEach(key => {
                    form[key] = record[key]
                })
            Object.keys(form).forEach(key => {
                params += `&${key}=${form[key]}`
            })
            exportOrderGoods(params)
        }
    }
}
</script>

<style lang="less"></style>
